USE [msdb]
GO

/****** Object:  Job [Back Up Database - Weeks]    Script Date: 05/16/2012 17:15:28 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [Database Maintenance]    Script Date: 05/16/2012 17:15:29 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Back Up Database - Weeks', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'No description available.', 
		@category_name=N'Database Maintenance', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Step_1]    Script Date: 05/16/2012 17:15:31 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Step_1', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=2, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'declare @iWeek int, @iModulo int, @sNameArcive varchar(10), 
		@sDesc varchar(100), @sName varchar(150), @sBaseName varchar(150);

SET DATEFIRST 1;

SET @iModulo = 4;
SET @iWeek = DATEPART(wk, GETDATE());

SET @iWeek = @iWeek % @iModulo		-- остаток от деления на 4. Определяем Кратный № недели.
IF @iWeek = 0				-- Если деление без остатка, значит четвертая неделя.
	SET @iWeek = @iModulo;

SET @sNameArcive = N''Week_'' + CAST(@iWeek as char(1));
SET @sDesc = N''Полная недельная резервная копия'';
SET @sName = @sDesc + N'': '';

---  первая строчка восстановления ИМЕННО В ЭТОМ БЕКАПЕ
---  долджна быть обязательно с опциями INIT, SKIP
---  все остальные обязательно NOINIT, NOSKIP

SET @sBaseName = N''I_Dealer-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [i_dealer]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, INIT,  NAME = @sBaseName, SKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''LOT-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [LOT]		TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10


SET @sBaseName = N''Cards-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [Cards]		TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Services-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [Services]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''SMS-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [SMS]		TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Wallet-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [Wallet]		TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Management-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [Management]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Profiles-'' + @sName + CONVERT(varchar(25),GETDATE(),120);
BACKUP DATABASE [Profiles]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName,	NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Quizes-'' + @sName + CONVERT(varchar(25),GETDATE(),120);																														   
BACKUP DATABASE [Quizes]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName, NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Sms_d-'' + @sName + CONVERT(varchar(25),GETDATE(),120);																															   
BACKUP DATABASE [Sms_d]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName, NOSKIP, REWIND, NOUNLOAD,  STATS = 10
						
SET @sBaseName = N''Sms_p-'' + @sName + CONVERT(varchar(25),GETDATE(),120);																													   
BACKUP DATABASE [Sms_p]	TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName, NOSKIP, REWIND, NOUNLOAD,  STATS = 10

SET @sBaseName = N''Shop-'' + @sName + CONVERT(varchar(25),GETDATE(),120);																													   
BACKUP DATABASE [Shop]		TO  @sNameArcive WITH  DESCRIPTION = @sDesc,  RETAINDAYS = 8, NOFORMAT, NOINIT,  NAME = @sBaseName, NOSKIP, REWIND, NOUNLOAD,  STATS = 10
GO', 
		@database_name=N'master', 
		@database_user_name=N'ALTER-I\AdministratorDB', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'EveryWeek', 
		@enabled=1, 
		@freq_type=8, 
		@freq_interval=2, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=1, 
		@active_start_date=20070508, 
		@active_end_date=99991231, 
		@active_start_time=0, 
		@active_end_time=235959, 
		@schedule_uid=N'a962f423-0a89-485c-bf2d-f90fe3025ff2'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'one to delete', 
		@enabled=0, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=0, 
		@active_start_date=20090207, 
		@active_end_date=20090208, 
		@active_start_time=3000, 
		@active_end_time=235959, 
		@schedule_uid=N'1ac4bb3a-f5d7-4ba1-9ac2-4b253d061e37'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

